package com.google.gwt.mobile.phonegap.api;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * The Class Compass.
 */
public class Compass {

  /**
   * Gets the current heading.
   * 
   * @param callback the callback
   * @return the current heading
   */
  public static native void getCurrentHeading(Callback callback) /*-{
		$wnd.navigator.compass
				.getCurrentHeading(
						function(heading) {
							callback.@com.google.gwt.mobile.phonegap.api.Compass.Callback::onSuccess(F)(heading);
						},
						function() {
							callback.@com.google.gwt.mobile.phonegap.api.Compass.Callback::onError()();
						});
  }-*/;

  /**
   * Watch heading.
   * 
   * @param callback the callback
   * @return the string
   */
  public static String watchHeading(final Callback callback) {
    return watchHeading(callback, (JavaScriptObject) null);
  }

  /**
   * Watch heading.
   * 
   * @param callback the callback
   * @param options the options
   * @return the string
   */
  public static String watchHeading(final Callback callback, final Options options) {
    return watchHeading(callback, options.getOptions());
  }

  private static native String watchHeading(Callback callback, JavaScriptObject options) /*-{
		var id = $wnd.navigator.compass
				.watchHeading(
						function(heading) {
							callback.@com.google.gwt.mobile.phonegap.api.Compass.Callback::onSuccess(F)(heading);
						},
						function() {
							callback.@com.google.gwt.mobile.phonegap.api.Compass.Callback::onError()();
						}, options);
		return id;
  }-*/;

  /**
   * Clear watch.
   * 
   * @param watchId the watch id
   */
  public static native void clearWatch(String watchId) /*-{
		$wnd.navigator.compass.clearWatch(watchId);
  }-*/;

  /**
   * The Interface Callback.
   */
  public interface Callback {
    void onSuccess(float heading);

    void onError();
  }

  /**
   * The Class Options.
   */
  public static class Options {

    private final Options self = this;
    private final JavaScriptObject options = JavaScriptObject.createObject();

    /**
     * Frequency.
     * 
     * @param f the f
     * @return the options
     */
    public native Options frequency(int f) /*-{
			this.@com.google.gwt.mobile.phonegap.api.Compass.Options::options.frequency = f;
			return this.@com.google.gwt.mobile.phonegap.api.Compass.Options::self;
    }-*/;

    private JavaScriptObject getOptions() {
      return options;
    }

  }

}
